const app = new Vue({
	el:'#app',
	data:{
		books:[{
			id:1,
			name:'《算法导论》',
			date:'2006-9',
			price:85.00,
			count:1
		},
		{
			id:2,
			name:'《UNIX编程导论》',
			date:'2006-2',
			price:59.00,
			count:1
		},
		{
			id:3,
			name:'《编程导论》',
			date:'2008-1',
			price:30.00,
			count:1
		},
		{
			id:4,
			name:'《代码导论》',
			date:'2004-1',
			price:185.00,
			count:1
		}
		]	
	},
	methods:{
		getfinalPrice(price){
			return '¥'+price.toFixed(2)
		},
		increment(index){
			this.books[index].count++
		},
		decrement(index){
			this.books[index].count--		
		},
		removeHandle(index){
			this.books.splice(index,1)
		
		}
	},
	filters:{
		showPrice(price){
			return '¥'+price.toFixed(2)
		}
	},
	computed:{
		totalPrice(){
			let totalPrice =0
			/* 第一种
			 for(let i =0;i<this.books.length;i++){
				 totalPrice+=this.books[i].price*this.books[i].count
			} */
			/* 第二种 
			for(let i in this.books){
				const book = this.books[i]
				totalPrice+=book.price*books.count
			} */
		/* 	//第三种
			for (let item of this.books){
				totalPrice+=item.price*item.count
			} */
			//reduce
			return this.books.reduce(function(preValue,n){
				return preValue+n.price*n.count
				
			},0)
			
			return totalPrice
			//for (let i in/of this.books)
			//reduce
		}
	}
	
})